package com.telenav.j2me.datatypes;

import com.telenav.j2me.datatypes.ProtocolBuffer;
import java.io.ByteArrayOutputStream;
import java.util.Vector;

public class ProtocolBufferUtil
{
  public static ProtocolBuffer[] toBufArray(byte[] paramArrayOfByte)
  {
    if ((paramArrayOfByte == null) || (paramArrayOfByte.length == 0))
      return null;
    Vector<ProtocolBuffer> localVector = new Vector<ProtocolBuffer>();
    int i = 0;
    ProtocolBuffer localObject;
    int j;
    while (paramArrayOfByte.length > i)
    {
      localObject = new ProtocolBuffer();
      j = readInt(paramArrayOfByte, i);
      i += 4;
      String str = new String(paramArrayOfByte, i, j);
      localObject.setObjType(str);
      i += j;
      int k = readInt(paramArrayOfByte, i);
      if (k >= 0)
      {
        i += 4;
        byte[] arrayOfByte = new byte[k];
        for (int l = 0; l < k; ++l)
          arrayOfByte[l] = paramArrayOfByte[(i++)];
        localObject.setBufferData(arrayOfByte);
      }
      localVector.addElement(localObject);
    }
    if (localVector.size() > 0)
    {
      ProtocolBuffer[] localObjects = new ProtocolBuffer[localVector.size()];
      for (j = 0; j < localObjects.length; ++j)
        localObjects[j] = ((ProtocolBuffer)localVector.elementAt(j));
      return localObjects;
    }
    return ((ProtocolBuffer[])null);
  }

  @SuppressWarnings("unused")
public static byte[] listToByteArray(Vector<ProtocolBuffer> paramVector)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      for (int i = 0; i < paramVector.size(); ++i)
      {
        ProtocolBuffer localProtocolBuffer = (ProtocolBuffer)paramVector.elementAt(i);
        byte[] arrayOfByte1 = localProtocolBuffer.getObjType().getBytes();
        byte[] arrayOfByte2 = new byte[4];
        writeInt(arrayOfByte2, arrayOfByte1.length, 0);
        localByteArrayOutputStream.write(arrayOfByte2);
        if (arrayOfByte1 != null)
          localByteArrayOutputStream.write(arrayOfByte1);
        byte[] arrayOfByte3 = localProtocolBuffer.getBufferData();
        byte[] arrayOfByte4 = new byte[4];
        write(arrayOfByte4, arrayOfByte3.length, 0);
        localByteArrayOutputStream.write(arrayOfByte4);
        if (arrayOfByte3 == null)
          continue;
        localByteArrayOutputStream.write(arrayOfByte3);
      }
    }
    catch (Exception localException)
    {
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static int readInt(byte[] paramArrayOfByte, int paramInt)
  {
    int i = 255;
    return ((i & paramArrayOfByte[paramInt]) + ((i & paramArrayOfByte[(paramInt + 1)]) << 8) + ((i & paramArrayOfByte[(paramInt + 2)]) << 16) + (paramArrayOfByte[(paramInt + 3)] << 24));
  }

  public static int write(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
  {
    if ((paramInt1 >= -128) && (paramInt1 <= 127))
    {
      paramArrayOfByte[paramInt2] = (byte)paramInt1;
      return 1;
    }
    if ((paramInt1 >= -32768) && (paramInt1 <= 32767))
    {
      writeShort(paramArrayOfByte, paramInt1, paramInt2);
      return 2;
    }
    if ((paramInt1 >= -8388608) && (paramInt1 <= 8388607))
    {
      paramArrayOfByte[paramInt2] = (byte)(paramInt1 & 0xFF);
      paramInt1 >>= 8;
      paramArrayOfByte[(paramInt2 + 1)] = (byte)(paramInt1 & 0xFF);
      paramInt1 >>= 8;
      paramArrayOfByte[(paramInt2 + 2)] = (byte)(paramInt1 & 0xFF);
      return 3;
    }
    writeInt(paramArrayOfByte, paramInt1, paramInt2);
    return 4;
  }

  public static void writeShort(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
  {
    paramArrayOfByte[paramInt2] = (byte)(paramInt1 & 0xFF);
    paramInt1 >>= 8;
    paramArrayOfByte[(paramInt2 + 1)] = (byte)(paramInt1 & 0xFF);
  }

  public static void writeInt(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
  {
    paramArrayOfByte[paramInt2] = (byte)(paramInt1 & 0xFF);
    paramInt1 >>= 8;
    paramArrayOfByte[(paramInt2 + 1)] = (byte)(paramInt1 & 0xFF);
    paramInt1 >>= 8;
    paramArrayOfByte[(paramInt2 + 2)] = (byte)(paramInt1 & 0xFF);
    paramInt1 >>= 8;
    paramArrayOfByte[(paramInt2 + 3)] = (byte)(paramInt1 & 0xFF);
  }
}

/* Location:           D:\workspace\tn7x_android\7x\branches\europa-international\apps\eclipse-env\android_workspace\telenav-dev-android\lib\pusu.jar
 * Qualified Name:     com.telenav.j2me.framework.util.ProtocolBufferUtil
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.5.3
 */